/*
 * RunData.cpp
 *
 *  Created on: Feb 10, 2011
 *      Author: dblyth
 */

#include "RunData.h"
#include "TTree.h"
#include <iostream>
using namespace std;

RunData::RunData(TFile* file, char* runName) {
	TTree* tree = (TTree*) file->Get(runName);
	dataBranch = tree->GetBranch("data");
	entryCount = dataBranch->GetEntries();

	TBranch* samplesBranch = tree->GetBranch("samplesPerChannel");
	samplesBranch->SetAddress(&samplesPerChannel);
	samplesBranch->GetEntry(0);

	TBranch* channelBranch = tree->GetBranch("channelMask");
	int chanMask;
	channelBranch->SetAddress(&chanMask);
	channelBranch->GetEntry(0);

	numChannels = 0;
	for (int i = 0; i < 8; i++)
		if (chanMask & (1 << i))
			numChannels++;
}

RunData::~RunData() {
	;
}

unsigned short** RunData::getEntry(long entryNum) {
	unsigned short data[numChannels][samplesPerChannel];
	dataBranch->SetAddress(&data);
	dataBranch->GetEntry(entryNum);

	unsigned short** returnData = new unsigned short*[numChannels];
	for (int i = 0; i < numChannels; i++) {
		returnData[i] = new unsigned short[samplesPerChannel];
		for (int j = 0; j < samplesPerChannel; j++)
			returnData[i][j] = data[i][j];
	}
	return returnData;
}

int RunData::getEntryCount() {
	return entryCount;
}

int RunData::getSamplesPerChannel() {
	return samplesPerChannel;
}

int RunData::getNumChannels() {
	return numChannels;
}
